<?php

class Admin extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Admin_m');
    }

    function index() {
        $this->login();
    }

    function login() {
        if ($this->session->userdata('user_id') && $this->session->userdata('group_id')) {
            redirect('admin_content');
        }
        $config = array(
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $email = $this->input->post('email');
                $password = md5($this->input->post('password'));

                if ($this->Admin_m->login($email, $password)) {
                    redirect('admin_content');
                } else {
                    $data['error'] = lang('error_login');
                    $this->load->view('admin/login', $data);
                }
            } else {
                $this->load->view('admin/login');
            }
        } else {
            $this->load->view('admin/login');
        }
    }

    public function logout() {
        $this->Admin_m->logout();
        redirect('admin');
    }

    public function change_pass($user_id) {
        is_admin();
        $this->session->set_userdata('current_url', current_url());
        $config = array(
            array(
                'field' => 'apass',
                'label' => lang('old_pass'),
                'rules' => 'required|md5'
            ),
            array(
                'field' => 'npass',
                'label' => lang('new_pass'),
                'rules' => 'required|matches[confirm_npass]|md5'
            ),
            array(
                'field' => 'confirm_npass',
                'label' => lang('conf_pass'),
                'rules' => 'required|md5'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $pass = $this->input->post('apass');
            $query = $this->Admin_m->is_password($pass, $user_id);
            if ($query) {
                $npass = $this->input->post('npass');
                $query1 = $this->Admin_m->change_pass($user_id, $npass);
                if ($query) {
                    $this->session->set_flashdata('error', lang('change_pass') . ' ' . lang('success'));
                } else {
                    $this->session->set_flashdata('error', lang('change_pass') . ' ' . lang('fail'));
                }
                redirect('admin_content');
            } else {
                $this->_data['error'] = lang('in_pass');
                $this->display_admin('admin/change_pass');
            }
        }else
            $this->display_admin('admin/change_pass');
    }

    function is_correct_captcha($captcha) {
        $expiration = time() - 1800;
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($captcha, $this->input->ip_address(), $expiration);
        $query = $this->db->query($sql, $binds);
        $row = $query->row();

        if ($row->count == 0) {
            $this->form_validation->set_message('is_correct_captcha', 'Please enter correct word in the picture!');
            return FALSE;
        }
        return TRUE;
    }

    public function _email_check($email) {
        if ($this->Admin_m->email_check($email)) {
            $this->form_validation->set_message('_email_check', $this->lang->line('user_error_email'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

